package com.example.lizhixin.object_three_team.Uitls;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.lizhixin.object_three_team.Bean.CommuntiyBean;
import com.example.lizhixin.object_three_team.R;

import java.util.List;

import static com.example.lizhixin.object_three_team.Uitls.DensityUtil.dip2px;

public class CommunityViewAdapter extends RecyclerView.Adapter<CommunityViewAdapter.CommunityViewHolder> {
    private Context mContext;
    private List<CommuntiyBean.DataBean> data;

    public CommunityViewAdapter(Context mContext, List<CommuntiyBean.DataBean> data) {
        this.mContext = mContext;
        this.data = data;
    }

    @NonNull
    @Override
    public CommunityViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.communtiy_showitem, viewGroup, false);
        return new CommunityViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommunityViewHolder communityViewHolder, final int position) {
        String title = data.get(position).getTitle();
        int num = data.get(position).getNum();
        String id = data.get(position).getId();
        String pic = data.get(position).getPic();
        communityViewHolder.TvAppName.setText(title);
        communityViewHolder.TvAppTopic.setText("话题:" + num);
        communityViewHolder.TvAppAttention.setText("关注:" + id);

        //设置圆角
        CornerTransformUtil transformation = new CornerTransformUtil(mContext, dip2px(mContext, 5));
        //4个false 4个角都为圆角
        transformation.setExceptCorner(false, false, false, false);

        Glide.with(mContext)
                .load(pic)
                .asBitmap()
                .skipMemoryCache(true)
                .transform(transformation)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .error(R.mipmap.ic_launcher)
                .into(communityViewHolder.ImgAppPicture);


        if (onRecyclerViewItemClickListener != null) {
            communityViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onRecyclerViewItemClickListener.onClick(position);
                }
            });
//            communityViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View v) {
//                     长按
//                    onRecyclerViewItemClickListener.onLongClick(position);
//                    return false;
//                }
//            });
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public class CommunityViewHolder extends RecyclerView.ViewHolder {
        ImageView ImgAppPicture;
        TextView TvAppName, TvAppTopic, TvAppAttention;
        LinearLayout ll_item_layout;


        public CommunityViewHolder(@NonNull View itemView) {
            super(itemView);
            ImgAppPicture = itemView.findViewById(R.id.img_app_picture);
            TvAppName = itemView.findViewById(R.id.tv_app_name);
            TvAppTopic = itemView.findViewById(R.id.tv_app_topic);
            TvAppAttention = itemView.findViewById(R.id.tv_app_attention);
            ll_item_layout = itemView.findViewById(R.id.ll_item_layout);
        }
    }

    private OnItemClickListener onRecyclerViewItemClickListener;

    public interface OnItemClickListener {
        void onClick(int position);

//        void onLongClick(int position);
    }

    public void setOnRecyclerViewItemClickListener(OnItemClickListener onItemClickListener) {
        this.onRecyclerViewItemClickListener = onItemClickListener;
    }
}
